package com.fly.common;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ForEachSleep
{
    /**
     * 验证stream使用sleep
     */
    @Test
    public void test1()
    {
        IntStream.range(0, 10).mapToObj(i -> String.valueOf(i)).forEach(i -> {
            try
            {
                log.info("{}", i);
                TimeUnit.MILLISECONDS.sleep(200);
            }
            catch (InterruptedException e)
            {
            }
        });
    }
    
    @Test
    public void test()
    {
        IntStream.rangeClosed(0, 10).forEach(i -> log.info("{}", i));
        
        // 负数取余
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (RandomUtils.nextBoolean() ? 1 : -1) * RandomUtils.nextInt(10, screenSize.width / 10);
        log.info("{} {}", x, x % 1000);
    }
}
